
$(document).ready(init);

function init(){
	
	initData();
	renderPage();
	bindEvent();
}

function initData(){
	
}

function bindEvent(){
	$(document).bind(_EVENT_PROMPT_INFO,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_WARNING,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_ERROR,ComponentUtil.promptMsg);
	
}

function renderPage(){
	
	var addBt = {text:'新增菜单项',iconCls:'icon-add',handler:popupAddWindow};
	var removeBt = {text:'删除菜单项',iconCls:'icon-cancel',handler:confirmRemove };
	var reloadBt = {text:'刷新',iconCls:'icon-reload',handler:reloadList};
	
	
	$("#functionItemTable").datagrid({
		striped: true,
		fitColumns:true,
		idField:'id',
		frozenColumns:[[
	        {field:'ck',checkbox:true}
	    ]],
	    sortName:'catalogTitle',
    	sortOrder:'asc',
		url:webappName+'/admin/functionManager/list',
		toolbar:[addBt,'-',removeBt,'-',reloadBt,'-'],
		loadMsg:'加载数据中,请稍等...',
		onDblClickRow:popupModifyWindow
	});
		
	$.messager.defaults = {ok:'确认',cancel:'取消'};
	
	
	$('#functionItemTabs').tabs();
	
	
	$("#privilegeTable").datagrid({
		striped: true,
		fitColumns:true,
		pagination:false,
		singleSelect:true,
		idField:'code',
		frozenColumns:[[
	        {field:'ck',checkbox:true}
	    ]],
	    sortName:'id',
    	sortOrder:'desc',
		url:webappName+'/admin/privilegeManager/listPrivilege',
		loadMsg:'加载数据中,请稍等...'
	});
	
	
	$("#componentTable").datagrid({
		striped: true,
		fitColumns:true,
		pagination:false,
		singleSelect:true,
		idField:'code',
		frozenColumns:[[
	        {field:'ck',checkbox:true}
	    ]],
	    sortName:'id',
    	sortOrder:'desc',
		url:webappName+'/admin/componentManager/list',
		loadMsg:'加载数据中,请稍等...'
	});
	
	$("#functionItemDetail").dialog({
		width:580,
		height:410,
		resizable:true,
		closed:true
	});
	
	
	
}

function statusRender(value,rowData,rowIndex){
	for (var i=0;i<_COMMON_STATUS_COMBO_SELECTOR.length;i++){
		if (value==_COMMON_STATUS_COMBO_SELECTOR[i].value){
			return _COMMON_STATUS_COMBO_SELECTOR[i].text;
		}
	} 
	return '未知('+value+')';
}

function popupAddWindow(){
	var addButton = {
		text:'新增菜单项',
		iconCls:'icon-add',
		handler:commitAddFunction
	};
	
	var cancelButton = {
		text:'取消',
		handler:closeDetailWindow
	};
	
	$("#functionItemDetail").dialog({
		closed:false,
		title:'新增菜单项',
		modal:true,
		buttons:[addButton,cancelButton]
	});
	
	$("#privilegeTable").datagrid('clearSelections');
	$("#componentTable").datagrid('clearSelections');
	$("#privilegeTable").datagrid('reload');
	$("#componentTable").datagrid('reload');
	$("#privilegeTable").datagrid('selectRow',0);
	$("#componentTable").datagrid('selectRow',0);
	$('#functionItemForm  [type=text]').val('');
}

function commitAddFunction(){
	if (!$('#functionItemForm').form('validate')) return;
	
	var component = $("#componentTable").datagrid('getSelected').code;
	var privilegeCode=$("#privilegeTable").datagrid('getSelected').code;
	var code=$('#functionItemCode').val();
	var title = $('#title').val();
	var catalogTitle = $('#catalogTitle').val();
	var status=0;
	
	var funItem = {component:component,privilegeCode:privilegeCode,code:code,title:title,catalogTitle:catalogTitle,status:status};
	
	$.post(webappName+"/admin/functionManager/add",$.param(funItem),onAddFunItemComplete,"json");
	
	closeDetailWindow();
	
}

function onAddFunItemComplete(responseVO){
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(responseVO.errCode));
	}
	reloadList();
}



function popupModifyWindow(rowIndex, rowData){
	var modifyButton = {
		text:'保存修改',
		iconCls:'icon-add',
		handler:function(){commitModifyFunction(rowData.id);}
	};
	
	var cancelButton = {
		text:'取消',
		handler:closeDetailWindow
	};
	
	$("#functionItemDetail").dialog({
		closed:false,
		title:'修改菜单项',
		modal:true,
		buttons:[modifyButton,cancelButton]
	});
	
	$('#functionItemCode').val(rowData.code);
	$('#title').val(rowData.title);
	$('#catalogTitle').val(rowData.catalogTitle);
	
	$("#privilegeTable").datagrid('clearSelections');
	$("#componentTable").datagrid('clearSelections');
	$("#privilegeTable").datagrid('reload');
	$("#componentTable").datagrid('reload');
	$("#privilegeTable").datagrid('selectRecord',rowData.privilegeCode);
	$("#componentTable").datagrid('selectRecord',rowData.component);
	
}

function commitModifyFunction(id){
	
	if (!$('#functionItemForm').form('validate')) return;
	
	
	var component = $("#componentTable").datagrid('getSelected').code;
	var privilegeCode=$("#privilegeTable").datagrid('getSelected').code;
	var code=$('#functionItemCode').val();
	var title = $('#title').val();
	var catalogTitle = $('#catalogTitle').val();
	var status=0;
	
	var funItem = {id:id,component:component,privilegeCode:privilegeCode,code:code,title:title,catalogTitle:catalogTitle,status:status};
	
	$.post(webappName+"/admin/functionManager/modify",$.param(funItem),onModifyFunItemComplete,"json");
	
	
	closeDetailWindow();
}

function onModifyFunItemComplete(responseVO){
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(responseVO.errCode));
	}
	reloadList();
	
}

function confirmRemove(){
	var rows = $('#functionItemTable').datagrid('getSelections');
	if (rows.length==0){
		$(document).trigger(_EVENT_PROMPT_INFO,'请选择需要删除的项目');
		return;
	}
	$.messager.confirm('确认','确认要删除吗?',removeFunItems);
}

function removeFunItems(isConfirm){
	if (!isConfirm) return;
	
	var rows = $('#functionItemTable').datagrid('getSelections');
	var idList='';
	for(var i=0;i<rows.length;i++){
		idList += rows[i].id+',';
	}
	
	idList = idList.substring(0,idList.length-1);
	
	var requestPara = 'idList='+idList;
	
	$.post(webappName+"/admin/functionManager/remove",requestPara,onRemoveFunItemComplete,"json");
}

function onRemoveFunItemComplete(responseVO){
	
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(responseVO.errCode));
	}
	reloadList();
	
}

function closeDetailWindow(){
	$("#functionItemDetail").dialog('close');
}
function reloadList(){
	$("#functionItemTable").datagrid('reload');
}